/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Client_GUI.SavingBook;

import Interface.IRemoteCustomer;
import Interface.IRemoteSavingBook;
import Interface.IRemoteSavingType;
import Server.DTO.Customer_DTO;
import Server.DTO.SavingBook_DTO;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 10520_000
 */
public class table_SavingBook extends AbstractTableModel {

    //global variable
    Registry registry;
    IRemoteSavingBook rs;
    IRemoteCustomer rc;
    IRemoteSavingType rsavingtype;
    DefaultTableModel model;
    private JTable table;

    public table_SavingBook() throws RemoteException, NotBoundException {
        registry = LocateRegistry.getRegistry("127.0.0.1");
        rs = (IRemoteSavingBook) registry.lookup("SavingBook");
        rc = (IRemoteCustomer) registry.lookup("Customer");
        rsavingtype = (IRemoteSavingType) registry.lookup("SavingType");
        LoadTable();
    }

    
    //Funtions 
    public DefaultTableModel LoadTable() throws RemoteException, NotBoundException {
        //add header cho bang
        Vector Header = new Vector();
        Vector vtData = new Vector();
        Header.add("Mã Sổ");
        Header.add("Tên Khách Hàng");
        Header.add("Số CMND");
        Header.add("Tên Loại Tiết Kiệm");
        Header.add("Kỳ Hạn");
        Header.add("Số Tiền Trong Tài Khoản");
        Header.add("Trạng Thái Của Sổ");
        Header.add("Ngày Mở Sổ");

        //gan resource cho bang
        ArrayList arr = rs.FindAll();
        for (Iterator it = arr.iterator(); it.hasNext();) {
            SavingBook_DTO info = (SavingBook_DTO) it.next();
            Vector vtrow = new Vector();
            vtrow.add(info.getBookID());
            vtrow.add(rc.FindByCustomerID(info.getCustomerID()).getFullName());
            vtrow.add(rc.FindByCustomerID(info.getCustomerID()).getIDNumber());
            vtrow.add(rsavingtype.FindByTypeID(info.getTypeID()).getName());
            vtrow.add(rsavingtype.FindByTypeID(info.getTypeID()).getTerm());
            vtrow.add(info.getAccountBalance());
            if(info.getSBStatus())
                vtrow.add("Mở");
            else
                vtrow.add("Đóng");
            vtrow.add(info.getOpeningDate());
            vtData.add(vtrow);
        }
        //tra ve bang
        return new DefaultTableModel(vtData, Header);
    }
    //
     //Funtions 
    public DefaultTableModel LoadTableSearch(String str_fullname) throws RemoteException, NotBoundException {
        //add header cho bang
        Vector Header = new Vector();
        Vector vtData = new Vector();
        Header.add("Mã Sổ");
        Header.add("Tên Khách Hàng");
        Header.add("Số CMND");
        Header.add("Tên Loại Tiết Kiệm");
        Header.add("Kỳ Hạn");
        Header.add("Số Tiền Trong Tài Khoản");
        Header.add("Trạng Thái Của Sổ");
        Header.add("Ngày Mở Sổ");

        //gan resource cho bang
        ArrayList arr = rs.GetSavingBooks(str_fullname);
        for (Iterator it = arr.iterator(); it.hasNext();) {
            SavingBook_DTO info = (SavingBook_DTO) it.next();
            Vector vtrow = new Vector();
            vtrow.add(info.getBookID());
            vtrow.add(rc.FindByCustomerID(info.getCustomerID()).getFullName());
            vtrow.add(rc.FindByCustomerID(info.getCustomerID()).getIDNumber());
            vtrow.add(rsavingtype.FindByTypeID(info.getTypeID()).getName());
            vtrow.add(rsavingtype.FindByTypeID(info.getTypeID()).getTerm());
            vtrow.add(info.getAccountBalance());
            if(info.getSBStatus())
                vtrow.add("Mở");
            else
                vtrow.add("Đóng");
            vtrow.add(info.getOpeningDate());
            vtData.add(vtrow);
        }
        //tra ve bang
        return new DefaultTableModel(vtData, Header);
    }

    @Override
    public int getRowCount() {
        return table.getRowCount();
    }

    @Override
    public int getColumnCount() {
        return table.getColumnCount();
    }

    @Override
    public Object getValueAt(int rowIndex, int columIndex) {
        return table.getValueAt(rowIndex, columIndex);

    }

    /**
     * @return the table
     */
    public JTable getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(JTable table) {
        this.table = table;
    }

}
